rm(list = ls())
x1 <- c(2, 4, -1, -2, 5)
x2 <- c("one", "two", "three")
x3 <- c(TRUE, FALSE, TRUE, FALSE)

x4 <- 1:5
x5 <- seq(from = 2, to = 100, by = 7)
x5[5]
x5[c(5, 8, 9)]#显示向量第5、8、9个元素

x <- x5
x[-(1:4)]

weight <- c(68, 72, 57, 90, 65, 81)
height <- c(1.75, 1.80, 1.65, 1.90, 1.72, 1.87)
bmi <- weight/height^2
bmi

length(bmi)
mean(bmi)
var(bmi)
sd(bmi)

sex <- c(1,2,1,1,2,1,2)
#利用函数factor（因子）将变量转换为对象并存储为sex.f
sex.f <- factor(sex, levels = c(1,2), labels = c ("Male", "Female"))
sex.f
#因子的属性可以用levels()来查看
#使用函数relevel（）可以改变因子水平的排列顺序以改变参考组
sex.f1 <- relevel(sex.f, ref = "Female")
sex.f1

#要表示有序因子，需在函数factor（）中指定参数ordered = TRUE
status <- c(1,2,2,3,1,2,2)
status.f <- factor(status,
                   levels = c(1,2,3),
                   labels = c("Poor", "Improved", "Rich"),
                   ordered = TRUE)
status.f

M <- matrix(1:6, nrow = 2)
M

mat1 <- matrix(1:6, nrow = 3)
mat1
mat2 <- matrix(5:10, nrow = 2)
mat2
dim(mat1)
dim(mat2)

mat1 %*% mat2

t(mat1)
mat3 <- matrix(1:4, nrow = 2)
#行列式
det(mat3)
#逆矩阵
solve(mat3)

rowSums(mat3)
colSums(mat3)
rowMeans(mat3)
colMeans(mat3)

dim1 <- c("A1","A2", "A3")
dim2 <- c("B1", "B2", "B3", "B4")
dim3 <- c("C1","C2")
array(1:24, 
      dim = c(3,4,2),
      dimnames = list(dim1, dim2,dim3))

list1 <- list(a = 1, b = 1:5, c = c("red", "blue","green"))
list1

set.seed(123)
dat <- rnorm(10)
bp <- boxplot(dat)
class(bp)
bp
bp$stats

x <- c(1,2,5)
is.numeric(x)
is.vector(x)
y <- as.character(x)
y
is.numeric(x)
is.character(x)
z <- c(TRUE, FALSE, TRUE, FALSE)
is.logical(z)
as.numeric(z)

data(package = "datasets")

library(MASS)
data(bacteria)
r1 <- rnorm(n = 100, mean = 0, sd = 1)
r2 <- runif(n = 10000, min = 0, max = 100)
r3 <- rbinom(n = 80, size = 100, prob = 0.1)
r4 <- rpois(n=50, lambda = 1)

patients.data <- read.table("patients.txt", header = TRUE)
patients.data
patients.data <- read.csv("patients.csv")
patients.data
#install.packages("openxlsx")
library(openxlsx)
patients.data <- read.xlsx("patients.xlsx", sheet = 1)
patients.data
#install.packages("foreign")
library(foreign)
patients.data <- read.spss("patients.sav", to.data.frame = TRUE)
View(patients.data)
#导出数据
write.csv(patients.data, file = "patients_data.csv")
save(patients.data, file = "patients.rdata")
load("patients.rdata")
#用rio包导入、导出数据
#install.packages("rio")
library(rio)
data("infert")
str(infert)
export(infert, "infert.csv")
convert("infert.csv", "infert.sav")
infert.data <- import("infert.sav")
infert.data$education <- as.factor(infert.data$education)

#创建一个包含三个变量的数据框。
#x：小写字母a-j
#y：数字1-10
#z：10个1
my_data <- data.frame(x = letters[1:10],
                      y = 1:10,
                      z = rep(1, 10))
#加载surviv包中的数据集lung，并查看其帮助文档
library(survival)
data(lung)
?lung
#生成服从正态分布的随机数
x <- rnorm(1000, mean = 168, sd = 10) 
hist(x)
#导出数据、读入该文件
library(datasets)
data("iris")
write.csv(iris, file = "iris.csv")
iris1 <- read.csv("iris.csv")
















